package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.api.system.domain.SysRegion;
import com.ruoyi.common.core.domain.Ztree;
import org.springframework.stereotype.Service;
import com.ruoyi.api.system.mapper.SysRegionMapper;
import com.ruoyi.system.service.ISysRegionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 行政区字典Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-08-19
 */
@Service
public class SysRegionServiceImpl extends ServiceImpl<SysRegionMapper, SysRegion> implements ISysRegionService
{

    /**
     * 查询行政区划管理树
     *
     * @return 所有部门信息
     */
    @Override
    public List<Ztree> selectRegionTree()
    {
        List<SysRegion> regionList = baseMapper.selectList(new LambdaQueryWrapper<SysRegion>().apply("region_level < {0}", "3"));
        List<Ztree> ztrees = initZtree(regionList);
        return ztrees;
    }

    /**
     * 查询行政区字典列表
     * 
     * @param sysRegion 行政区字典
     * @return 行政区字典
     */
    @Override
    public List<SysRegion> selectSysRegionList(SysRegion sysRegion)
    {
        return baseMapper.selectSysRegionList(sysRegion);
    }

    /**
     * 对象转树
     *
     * @param regionList 行政区划列表
     * @return 树结构列表
     */
    public List<Ztree> initZtree(List<SysRegion> regionList)
    {
        List<Ztree> ztrees = new ArrayList<Ztree>();
        for (SysRegion region : regionList)
        {
            Ztree ztree = new Ztree();
            ztree.setId(region.getRegionId());
            ztree.setpId(region.getParentId());
            ztree.setName(region.getRegionName());
            ztree.setTitle(region.getRegionName());
            ztrees.add(ztree);
        }
        return ztrees;
    }
}
